// pages/cateringDishesEdit/cateringDishesEdit.js
import { HOST, UPLOAD_HOST } from '../../config/apiConfig.js'
const { extend, Field, Toast } = require('../../zanui/index')
import { obj2Form } from '../../utils/util.js'

const app = getApp()

Page(extend({}, Field, Toast, {

  /**
   * 页面的初始数据
   */
  data: {
    submitDisabled: false,
    belongCateringId: null,
    id: null,
    dishesImages: '../../assets/img/add_img.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: options.dishesId ? '修改推荐菜' : '添加推荐菜',
    })
    this.setData({
      belongCateringId: options.cateringId,
      id: options.dishesId,
    })
    if (options.dishesId) {
      this.getDishesInfo()
    }
  },
  selectImgs(e) {
    const _self = this
    wx.chooseImage({
      sizeType: ['original'],
      count: 1,
      success: function (res) {
        wx.showLoading({
          title: '上传中...',
        })
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: UPLOAD_HOST,
          filePath: tempFilePaths[0],
          name: 'file',
          success: function (res) {
            wx.hideLoading()
            let data = JSON.parse(res.data)
            _self.setData({
              dishesImages: data.data[0].downloadPath
            })
          }
        })
      }
    })
  },/**
   * 提交数据
   */
  formSubmit: function (event) {
    //追加单选钮数据
    const formData = event.detail.value
    if (this.data.dishesImages.indexOf('../assets') > -1) {
      this.showZanToast('请上传图片')
      return
    }
    formData.dishesImages = this.data.dishesImages
    for (let key in formData) {
      if (formData[key] == '' && key != 'dishesNumber' && key != 'dishesIntro') {
        this.showZanToast('数据不能为空')
        return
      }
    }
    formData.belongCateringId = this.data.belongCateringId
    this.setData({
      submitDisabled: true
    })
    wx.showLoading({
      title: '加载中',
    });

    const _self = this
    let requestMethod = 'POST'
    let requestUrl = HOST + '/v1/recommendedDishes'
    if (this.data.id) {
      // 更新
      formData.updateBy = app.globalData.userInfo.openid;
      requestMethod = 'PUT'
      requestUrl = HOST + '/v1/recommendedDishes/' + this.data.id
    } else {
      // 添加
      formData.createBy = app.globalData.userInfo.openid;
    }
    wx.request({
      url: requestUrl,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: obj2Form(formData),
      method: requestMethod,
      success: function (res) {
        console.log(res)
        if (res.statusCode != 200) {
          _self.showZanToast('请求接口失败')
          return
        }
        if (res.data.code != '000000') {
          _self.showZanToast(res.data.message)
          return
        }
        wx.navigateBack({})
      },
      fail: function () {
        _self.showZanToast('网络错误')
      },
      complete: function () {
        _self.setData({
          submitDisabled: false
        })
        wx.hideLoading()
      }
    })
  },
  getDishesInfo () {
    const _self = this
    wx.request({
      url: 'https://qdndc.ideatower.cn/v1/recommendedDishes',
      data: {
        id: this.data.id
      },
      success: (res) => {
        console.log(res)
        if (res.statusCode == 200 && res.data.code == '000000') {
          const data = res.data.data[0]
          this.setData(data)
        } else {
          this.showZanToast('获取数据失败，请返回重新获取')
        }
      },
      fail: (err) => {
        this.showZanToast('获取数据失败，请返回重新获取')
      }
    })
  }
}))